package org.xxpay.manage.alipay.cash.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.xxpay.core.common.annotation.MethodLog;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.MchConstant;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.domain.BizResponse;
import org.xxpay.core.common.domain.XxPayPageRes;
import org.xxpay.core.common.domain.XxPayResponse;
import org.xxpay.core.entity.AlipayCashUser;
import org.xxpay.manage.common.ctrl.BaseController;
import org.xxpay.manage.common.service.CommonService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/alipayCashUser")
public class AlipayCashUserController extends BaseController {

    @Autowired
    private CommonService commonService;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        AlipayCashUser record = getObject(param, AlipayCashUser.class);

        if(getUser().getIsSuperAdmin() != MchConstant.PUB_YES) record.setCreateUserId(getUser().getId()); //非超管需要添加查询条件

        int count = commonService.alipayCashUserService.count(record);
        if(count == 0) return ResponseEntity.ok(XxPayPageRes.buildSuccess());
        List<AlipayCashUser> recordList = commonService.alipayCashUserService.select((getPageIndex(param) - 1) * getPageSize(param), getPageSize(param), record);
        return ResponseEntity.ok(XxPayPageRes.buildSuccess(recordList, count));
    }

    /**
     * 查询信息
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        int id = getInteger(param, "id");
        AlipayCashUser record = commonService.alipayCashUserService.findById(id);

        //避免越权查询
        if(getUser().getIsSuperAdmin() != MchConstant.PUB_YES && !getUser().getId().equals(record.getCreateUserId())){
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_RECORD_NOT_EXIST));
        }

        return ResponseEntity.ok(XxPayResponse.buildSuccess(record));
    }

    @RequestMapping("/add")
    @ResponseBody
    @MethodLog( remark = "添加分账用户" )
    public ResponseEntity<?> add(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long totalMoneyLimit = getRequiredAmountL(param, "totalMoneyLimit");
        param.put("totalMoneyLimit", totalMoneyLimit);
        AlipayCashUser alipayCashUser = getObject(param, AlipayCashUser.class);
        List<AlipayCashUser> listCashUser = commonService.alipayCashUserService.findByAlipayLoginId(alipayCashUser.getAlipayLoginId());
        if (listCashUser.size() >= 1){
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_MGR_ALIPAY_ACCOUNT_USED));
        }
        alipayCashUser.setCreateUserId(getUser().getId());
        int count = commonService.alipayCashUserService.add(alipayCashUser);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    @RequestMapping("/update")
    @ResponseBody
    @MethodLog( remark = "修改分账用户" )
    public ResponseEntity<?> update(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        handleParamAmount(param, "totalMoneyLimit");
        AlipayCashUser updateRecord = getObject(param, AlipayCashUser.class);
        AlipayCashUser cashUser = commonService.alipayCashUserService.findById(updateRecord.getId());

        //避免越权操作
        if(getUser().getIsSuperAdmin() != MchConstant.PUB_YES && !getUser().getId().equals(cashUser.getCreateUserId())){
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_RECORD_NOT_EXIST));
        }

        if(updateRecord.getStatus() == AlipayCashUser.STATE_AVAILABLE && cashUser.getTotalMoney() >= updateRecord.getTotalMoneyLimit()){
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_MGR_ALIPAY_USER_LT_LIMIT));
        }

        int count = commonService.alipayCashUserService.update(updateRecord);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    /**
     * 删除分账用户
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseEntity<?> delete(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        int id = getInteger(param, "id");
        int resule = commonService.alipayCashUserService.delete(id);
        return ResponseEntity.ok(XxPayResponse.buildSuccess(resule));
    }


    /** 查询可用分账条数 **/
    @RequestMapping("/countByAvailable")
    @ResponseBody
    public ResponseEntity<?> countByAvailable(HttpServletRequest request) {
        return ResponseEntity.ok(XxPayResponse.buildSuccess(commonService.alipayCashUserService.selectCountByMgrUser()));
    }


}
